import request from "@/utils/request";
import { AxiosPromise } from "axios";
import {
  CaptchaResult,
  LoginData,
  LoginResult,
  AuthQuery,
  AuthPageVO,
  AuthForm,
  ResouceQuery,
  ResoucePageVO,
} from "./types";

/**
 * 登录API
 *
 * @param data {LoginData}
 * @returns
 */
export function loginApi(data: LoginData): AxiosPromise<LoginResult> {
  const formData = new FormData();
  formData.append("username", data.username);
  formData.append("password", data.password);
  formData.append("captchaKey", data.captchaKey || "");
  formData.append("captchaCode", data.captchaCode || "");
  return request({
    url: "/api/v1/auth/login",
    method: "post",
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
}

/**
 * 注销API
 */
export function logoutApi() {
  return request({
    url: "/api/v1/auth/logout",
    method: "delete",
  });
}

/**
 * 获取验证码
 */
export function getCaptchaApi(): AxiosPromise<CaptchaResult> {
  return request({
    url: "/api/v1/auth/captcha",
    method: "get",
  });
}

export function getResoucesPage(
  queryParams: ResouceQuery
): AxiosPromise<PageResult<ResoucePageVO[]>> {
  return request({
    url: "/api/car/resources/page",
    method: "get",
    params: queryParams,
  });
}

export function getResoucesImgPage(
  queryParams: ResouceQuery
): AxiosPromise<PageResult<ResoucePageVO[]>> {
  return request({
    url: "/api/car/resources/pageImg",
    method: "get",
    params: queryParams,
  });
}

export function getAuthPage(
  queryParams: AuthQuery
): AxiosPromise<PageResult<AuthPageVO[]>> {
  return request({
    url: "/api/car/auth/page",
    method: "get",
    params: queryParams,
  });
}

export function getBrandList(): AxiosPromise<PageResult<AuthPageVO[]>> {
  return request({
    url: "/api/car/brand/brandList",
    method: "get",
  });
}

export function updateAuth(data: AuthForm) {
  return request({
    url: "/api/car/auth/authCar",
    method: "put",
    data: data,
  });
}

export function updateResources(data: AuthForm) {
  return request({
    url: "/api/car/resources/saveResources",
    method: "put",
    data: data,
  });
}

export function saveResourcesImg(data: AuthForm) {
  return request({
    url: "/api/car/resources/saveResourcesImg",
    method: "put",
    data: data,
  });
}

/**
 * 删除认证
 *
 * @param ids
 */
export function deleteAuth(ids: string) {
  return request({
    url: "/api/car/auth/" + ids,
    method: "delete",
  });
}

export function deleteResources(ids: string) {
  return request({
    url: "/api/car/resources/" + ids,
    method: "delete",
  });
}

export function deleteResourcesImg(ids: string) {
  return request({
    url: "/api/car/resources/del/" + ids,
    method: "delete",
  });
}

/**
 * 下载认证导入模板
 *
 * @returns
 */
export function downloadTemplateAppApi() {
  return request({
    url: "/api/car/auth/templateAuth",
    method: "get",
    responseType: "arraybuffer",
  });
}

/**
 * 导出APP认证
 *
 * @param 导出认证
 * @returns
 */
export function exportAuth(queryParams: AuthQuery) {
  return request({
    url: "/api/car/auth/expAuth",
    method: "get",
    params: queryParams,
    responseType: "arraybuffer",
  });
}

/**
 * 下载认证导入模板
 *
 * @returns
 */
export function downloadTemplateResouceApi() {
  return request({
    url: "/api/car/resources/templateResources",
    method: "get",
    responseType: "arraybuffer",
  });
}

/**
 * 导出APP认证
 *
 * @param 导出认证
 * @returns
 */
export function exportResource(queryParams: ResouceQuery) {
  return request({
    url: "/api/car/resources/expResources",
    method: "get",
    params: queryParams,
    responseType: "arraybuffer",
  });
}
